package log

import (
	"io"
	"irisfarm/app/helper"
	"log"
	"os"
	"time"
)

func New() *log.Logger {
	checkStorageLogsPath()

	logFile, err := os.OpenFile(appFilename(), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		panic(err)
	}

	return log.New(io.MultiWriter(logFile, os.Stderr), "", log.Ldate|log.Lmicroseconds|log.Lshortfile)
}

func appFilename() string {
	fileName := time.Now().Format("storage/logs/app-2006-01-02") + ".log"
	return fileName
}

func checkStorageLogsPath() {
	if !helper.FileExist(helper.BasePath() + "/storage/logs") {
		if ok := os.MkdirAll(helper.BasePath()+"/storage/logs", 0777); ok != nil {
			panic(ok)
		}
	}
}
